﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Expressing_Label
{
    public partial class VideoContentMiFrm : Form
    {
        private bool c_bMoving = false;
        private Point c_pStart = new Point();
        private Point[] poin = new Point[8];
        private Bitmap c_bmpFormBackground = null;
        private Dictionary<string, int> c_dicModelIndex = new Dictionary<string, int>();


        public VideoContentMiFrm()
        {
            InitializeComponent();
            poin[0] = new Point(40, 0);
            poin[1] = new Point(Width, 0);
            poin[2] = new Point(Width, Height);
            poin[3] = new Point(40, Height);
            poin[4] = new Point(40, ConfigurationFrm.g_yVideoButton + 20);
            poin[5] = new Point(20, ConfigurationFrm.g_yVideoButton);
            poin[6] = new Point(40, ConfigurationFrm.g_yVideoButton - 20);
            poin[7] = new Point(40, 0);

            IntPtr hRgn = CreatePolygonRgn(ref poin[0], 16, 1);
            SetWindowRgn(Handle, hRgn, true);

            c_bmpFormBackground = new Bitmap(Width, Height);
            Graphics gra = Graphics.FromImage(c_bmpFormBackground);
            gra.Clear(Color.WhiteSmoke);
            Pen p = new Pen(Color.Purple, 4);
            for (int i = 0; i < 7; i++)
            {
                gra.DrawLine(p, poin[i], poin[i + 1]);
            }
            this.BackgroundImage = c_bmpFormBackground;
            gra.Dispose();

            for (int i = 0; i < PublicResources.g_VideoModelItem.Count; i++)
            {
                if (PublicResources.g_VideoModelItem[i].StartsWith("#"))
                {
                    string ts = PublicResources.g_VideoModelItem[i];
                    ts = ts.Remove(0, 1);
                    c_dicModelIndex.Add(ts, i);
                    VideoModel_lst.Items.Add(ts);
                }
            }

        }

        [DllImport("gdi32.dll")]
        private static extern IntPtr CreatePolygonRgn(ref Point lpPoint, int nCount, int nPolyFillMode);

        [DllImport("user32.dll")]
        private static extern IntPtr SetWindowRgn(IntPtr hWnd, IntPtr hRgn, Boolean bReDraw);

        private void VideoContentMiFrm_MouseDown(object sender, MouseEventArgs e)
        {
            c_bMoving = true;
            c_pStart = MousePosition;
        }

        private void VideoContentMiFrm_MouseMove(object sender, MouseEventArgs e)
        {
            if(c_bMoving)
            {
                int x = MousePosition.X - c_pStart.X;
                int y = MousePosition.Y - c_pStart.Y;
                this.Left += x;
                this.Top += y;
                this.Owner.Left += x;
                this.Owner.Top += y;
                c_pStart = MousePosition;
            }
        }

        private void VideoContentMiFrm_MouseUp(object sender, MouseEventArgs e)
        {
            c_bMoving = false;
        }

        private void VideoContentMiFrm_MouseLeave(object sender, EventArgs e)
        {
            c_bMoving = false;
        }

        private void VideoContentMiFrm_Load(object sender, EventArgs e)
        {
            this.Left = this.Owner.Left + this.Owner.Width - 40;
            this.Top = this.Owner.Top;
        }

        private void VideoContentMiFrm_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen p = new Pen(Color.Blue, 4);
            for (int i = 0; i < 7; i++)
            {
                g.DrawLine(p, poin[i], poin[i + 1]);
            }
        }

        private void VideoModel_lst_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (VideoModel_lst.SelectedIndex < VideoModel_lst.Items.Count && VideoModel_lst.SelectedIndex != -1)
            {
                ModelDetail_lst.Items.Clear();
                string ts = VideoModel_lst.SelectedItem.ToString();
                int t = c_dicModelIndex[ts];
                int tt = PublicResources.g_VideoModelItem.Count;
                if(VideoModel_lst.SelectedIndex != VideoModel_lst.Items.Count -1)
                {
                    tt = c_dicModelIndex[VideoModel_lst.Items[VideoModel_lst.SelectedIndex +1].ToString()];
                }
                for (int i = t + 1; i < tt; i++)
                {
                    ModelDetail_lst.Items.Add(PublicResources.g_VideoModelItem[i]);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (ModelDetail_lst.Items.Count == 0)
            {
                MessageBox.Show("模型有误，请联系秋月大人！");
            }
            else
            {
                PublicResources.g_VideoModelDetail.Clear();
                for (int i = 0; i < ModelDetail_lst.Items.Count; i++)
                {
                    PublicResources.g_VideoModelDetail.Add(ModelDetail_lst.Items[i].ToString());
                }
                ConfigurationFrm.g_blSendByMiFrm = true;
                ConfigurationFrm.g_iNextFrm = 2;
                if (PublicResources.g_iConfiguraState == 1)
                    PublicResources.g_iConfiguraState = 2;
                this.Close();
            }
            
            
        }


    }
}
